package com.hj.dapp.business.sign.service.impl;

import com.hj.dapp.business.oss.model.FileUpLoadDto;
import com.hj.dapp.business.oss.model.FileUploadVo;
import com.hj.dapp.business.oss.service.OssService;
import com.hj.dapp.business.sign.model.SignDto;
import com.hj.dapp.business.sign.model.SignVo;
import com.hj.dapp.business.sign.service.SignService;
import com.hj.dapp.common.util.Base64Util;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;

import static com.hj.dapp.common.constant.OssConstant.CONCAT_EXT_NAME;
import static com.hj.dapp.common.constant.OssConstant.TYPE_CONTACT;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/2
 */
@Service
public class SignServiceImpl implements SignService {
    @Autowired
    private OssService ossService;

    @Override
    public SignVo signContact(SignDto signDto) {
        String PATH = "C:\\Users\\Administrator\\Desktop\\装修轻辅合同.pdf";
        try {
            String base64FileStr = signDto.getSign();
            String img = Base64Util.rotateImage(base64FileStr, 270);

            //        1、获得PDF文件流对象
            FileInputStream pdfInputStream = new FileInputStream(PATH);
            //        2、得到PDF文档对象
            PDDocument pdDocument = PDDocument.load(pdfInputStream);
            //        3、由Document得到Page对象
            PDPage page = pdDocument.getPage(0);
            //        4、通过图片路径和PDF文档对象得到图片image对象
            PDImageXObject image = LosslessFactory.createFromImage(pdDocument,Base64Util.base64ToBufferedImage(img));
            //        5、创建pageStream对象
            PDPageContentStream pageStream = new PDPageContentStream(pdDocument, page,PDPageContentStream.AppendMode.APPEND,false,false);
            //   6、pageStream对象绘制图片位置及大小，已PDF文件右下角为原点（x,y）是图片左下角左边，width、height是图片的长和宽
            pageStream.drawImage(image, 153, 642,70,30);

            pageStream.close();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            //    7、保存PDF到指定路劲
            pdDocument.save(byteArrayOutputStream );
            //      8、关闭流
            pdDocument.close();
            //获取输入流
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            FileUpLoadDto fileUpLoadDto = new FileUpLoadDto();
            fileUpLoadDto.setType(TYPE_CONTACT);
            fileUpLoadDto.setInputStream(inputStream);
            fileUpLoadDto.setExtName(CONCAT_EXT_NAME);
            FileUploadVo fileUploadVo = ossService.fileUpload(fileUpLoadDto);
            pdfInputStream.close();
            SignVo sinVo= new SignVo();
            BeanUtils.copyProperties(fileUploadVo,sinVo);
            return sinVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
